(function() {
  var express, iced, pgsql, router, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  router = express.Router();

  module.exports = router;

  router.get('/', function(req, res, next) {
    var client, done, err, merch, result, term, terms_input, terms_rejected, terms_wait_check, terms_wait_effect, uuids, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '站内通知'
    });
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 12
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
          });
          client.query("select * from web_users where uuid <> $1 and disabled = false", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 19
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.users = result.rows;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
            });
            client.query("select m.*, u.realname as username from web_messages as m left join web_users as u on m.sender = u.uuid where m.status = 1 and m.recver = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 29
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            res.locals.notify.messages = result.rows;
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
              });
              client.query("select m.*, u.realname as username from web_messages as m left join web_users as u on m.recver = u.uuid where m.status = 1 and m.sender = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 39
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              res.locals.notify.messages_send = result.rows;
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
                });
                client.query("select * from web_notify where status = 1 order by serial desc", __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 46
                }));
                __iced_deferrals._fulfill();
              })(function() {
                if (err) {
                  done(client);
                  return next(err);
                }
                res.locals.notify.notifications = result.rows;
                (function(__iced_k) {
                  var _i, _len, _ref;
                  if (req.session.notify.merchs_wait_check.length > 0) {
                    uuids = '';
                    _ref = req.session.notify.merchs_wait_check;
                    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                      merch = _ref[_i];
                      uuids += "'" + merch.uuid + "',";
                    }
                    uuids = uuids.substring(0, uuids.length - 1);
                    (function(__iced_k) {
                      __iced_deferrals = new iced.Deferrals(__iced_k, {
                        parent: ___iced_passed_deferral,
                        filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
                      });
                      client.query("select m.*, u.realname as input_user from pay_merch_ex as m left join web_users as u on u.uuid = m.inputer where m.uuid in ( " + uuids + " )", __iced_deferrals.defer({
                        assign_fn: (function() {
                          return function() {
                            err = arguments[0];
                            return result = arguments[1];
                          };
                        })(),
                        lineno: 59
                      }));
                      __iced_deferrals._fulfill();
                    })(function() {
                      if (err) {
                        done(client);
                        return next(err);
                      }
                      return __iced_k(res.locals.notify.merchs_wait_check = result.rows);
                    });
                  } else {
                    return __iced_k();
                  }
                })(function() {
                  (function(__iced_k) {
                    var _i, _len, _ref;
                    if (req.session.notify.merchs_rejected.length > 0) {
                      uuids = '';
                      _ref = req.session.notify.merchs_rejected;
                      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                        merch = _ref[_i];
                        uuids += "'" + merch.uuid + "',";
                      }
                      uuids = uuids.substring(0, uuids.length - 1);
                      (function(__iced_k) {
                        __iced_deferrals = new iced.Deferrals(__iced_k, {
                          parent: ___iced_passed_deferral,
                          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
                        });
                        client.query("select m.*, u.realname as check_user from pay_merch_ex as m left join web_users as u on u.uuid = m.checker where m.uuid in ( " + uuids + " )", __iced_deferrals.defer({
                          assign_fn: (function() {
                            return function() {
                              err = arguments[0];
                              return result = arguments[1];
                            };
                          })(),
                          lineno: 72
                        }));
                        __iced_deferrals._fulfill();
                      })(function() {
                        if (err) {
                          done(client);
                          return next(err);
                        }
                        return __iced_k(res.locals.notify.merchs_rejected = result.rows);
                      });
                    } else {
                      return __iced_k();
                    }
                  })(function() {
                    (function(__iced_k) {
                      var _i, _len, _ref;
                      if (req.session.notify.merchs_checked.length > 0) {
                        uuids = '';
                        _ref = req.session.notify.merchs_checked;
                        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                          merch = _ref[_i];
                          uuids += "'" + merch.uuid + "',";
                        }
                        uuids = uuids.substring(0, uuids.length - 1);
                        (function(__iced_k) {
                          __iced_deferrals = new iced.Deferrals(__iced_k, {
                            parent: ___iced_passed_deferral,
                            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
                          });
                          client.query("select m.*, u.realname as check_user from pay_merch_ex as m left join web_users as u on u.uuid = m.checker where m.uuid in ( " + uuids + " )", __iced_deferrals.defer({
                            assign_fn: (function() {
                              return function() {
                                err = arguments[0];
                                return result = arguments[1];
                              };
                            })(),
                            lineno: 85
                          }));
                          __iced_deferrals._fulfill();
                        })(function() {
                          if (err) {
                            done(client);
                            return next(err);
                          }
                          return __iced_k(res.locals.notify.merchs_checked = result.rows);
                        });
                      } else {
                        return __iced_k();
                      }
                    })(function() {
                      (function(__iced_k) {
                        var _i, _len, _ref;
                        if (req.session.notify.merchs_input.length > 0) {
                          uuids = '';
                          _ref = req.session.notify.merchs_input;
                          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                            merch = _ref[_i];
                            uuids += "'" + merch.uuid + "',";
                          }
                          uuids = uuids.substring(0, uuids.length - 1);
                          (function(__iced_k) {
                            __iced_deferrals = new iced.Deferrals(__iced_k, {
                              parent: ___iced_passed_deferral,
                              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
                            });
                            client.query("select m.* from pay_merch_ex as m where m.uuid in ( " + uuids + " )", __iced_deferrals.defer({
                              assign_fn: (function() {
                                return function() {
                                  err = arguments[0];
                                  return result = arguments[1];
                                };
                              })(),
                              lineno: 96
                            }));
                            __iced_deferrals._fulfill();
                          })(function() {
                            if (err) {
                              done(client);
                              return next(err);
                            }
                            return __iced_k(res.locals.notify.merchs_input = result.rows);
                          });
                        } else {
                          return __iced_k();
                        }
                      })(function() {
                        (function(__iced_k) {
                          var _i, _len, _ref;
                          if (req.session.notify.merchs_wait_effect.length > 0) {
                            uuids = '';
                            _ref = req.session.notify.merchs_wait_effect;
                            for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                              merch = _ref[_i];
                              uuids += "'" + merch.uuid + "',";
                            }
                            uuids = uuids.substring(0, uuids.length - 1);
                            (function(__iced_k) {
                              __iced_deferrals = new iced.Deferrals(__iced_k, {
                                parent: ___iced_passed_deferral,
                                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
                              });
                              client.query("select m.* from pay_merch_ex as m where m.uuid in ( " + uuids + " )", __iced_deferrals.defer({
                                assign_fn: (function() {
                                  return function() {
                                    err = arguments[0];
                                    return result = arguments[1];
                                  };
                                })(),
                                lineno: 107
                              }));
                              __iced_deferrals._fulfill();
                            })(function() {
                              if (err) {
                                done(client);
                                return next(err);
                              }
                              return __iced_k(res.locals.notify.merchs_wait_effect = result.rows);
                            });
                          } else {
                            return __iced_k();
                          }
                        })(function() {
                          terms_wait_check = [];
                          (function(__iced_k) {
                            var _i, _len, _ref, _results, _while;
                            _ref = req.session.notify.terms_wait_check;
                            _len = _ref.length;
                            _i = 0;
                            _while = function(__iced_k) {
                              var _break, _continue, _next;
                              _break = __iced_k;
                              _continue = function() {
                                return iced.trampoline(function() {
                                  ++_i;
                                  return _while(__iced_k);
                                });
                              };
                              _next = _continue;
                              if (!(_i < _len)) {
                                return _break();
                              } else {
                                term = _ref[_i];
                                (function(__iced_k) {
                                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                                    parent: ___iced_passed_deferral,
                                    filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
                                  });
                                  client.query("select t.*, u.realname as input_user, m.name as mer_name, m.merno as mer_no from pay_terms_ex as t left join web_users as u on t.inputer = u.uuid left join pay_merch as m on m.uuid = t.merid where t.uuid = $1", [term.uuid], __iced_deferrals.defer({
                                    assign_fn: (function() {
                                      return function() {
                                        err = arguments[0];
                                        return result = arguments[1];
                                      };
                                    })(),
                                    lineno: 122
                                  }));
                                  __iced_deferrals._fulfill();
                                })(function() {
                                  if (err) {
                                    done(client);
                                    return next(err);
                                  }
                                  return _next(terms_wait_check.push.apply(terms_wait_check, result.rows));
                                });
                              }
                            };
                            _while(__iced_k);
                          })(function() {
                            res.locals.notify.terms_wait_check = terms_wait_check;
                            terms_rejected = [];
                            (function(__iced_k) {
                              var _i, _len, _ref, _results, _while;
                              _ref = req.session.notify.terms_rejected;
                              _len = _ref.length;
                              _i = 0;
                              _while = function(__iced_k) {
                                var _break, _continue, _next;
                                _break = __iced_k;
                                _continue = function() {
                                  return iced.trampoline(function() {
                                    ++_i;
                                    return _while(__iced_k);
                                  });
                                };
                                _next = _continue;
                                if (!(_i < _len)) {
                                  return _break();
                                } else {
                                  term = _ref[_i];
                                  (function(__iced_k) {
                                    __iced_deferrals = new iced.Deferrals(__iced_k, {
                                      parent: ___iced_passed_deferral,
                                      filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
                                    });
                                    client.query("select t.*, m.name as mer_name, m.merno as mer_no from pay_terms_ex as t left join pay_merch as m on m.uuid = t.merid where uuid = $1", [term.uuid], __iced_deferrals.defer({
                                      assign_fn: (function() {
                                        return function() {
                                          err = arguments[0];
                                          return result = arguments[1];
                                        };
                                      })(),
                                      lineno: 136
                                    }));
                                    __iced_deferrals._fulfill();
                                  })(function() {
                                    if (err) {
                                      done(client);
                                      return next(err);
                                    }
                                    return _next(terms_rejected.push.apply(terms_rejected, result.rows));
                                  });
                                }
                              };
                              _while(__iced_k);
                            })(function() {
                              res.locals.notify.terms_wait_check = terms_wait_check;
                              terms_input = [];
                              (function(__iced_k) {
                                var _i, _len, _ref, _results, _while;
                                _ref = req.session.notify.terms_input;
                                _len = _ref.length;
                                _i = 0;
                                _while = function(__iced_k) {
                                  var _break, _continue, _next;
                                  _break = __iced_k;
                                  _continue = function() {
                                    return iced.trampoline(function() {
                                      ++_i;
                                      return _while(__iced_k);
                                    });
                                  };
                                  _next = _continue;
                                  if (!(_i < _len)) {
                                    return _break();
                                  } else {
                                    term = _ref[_i];
                                    (function(__iced_k) {
                                      __iced_deferrals = new iced.Deferrals(__iced_k, {
                                        parent: ___iced_passed_deferral,
                                        filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
                                      });
                                      client.query("select t.*, m.name as mer_name, m.merno as mer_no from pay_terms_ex as t left join pay_merch as m on m.uuid = t.merid where t.uuid = $1", [term.uuid], __iced_deferrals.defer({
                                        assign_fn: (function() {
                                          return function() {
                                            err = arguments[0];
                                            return result = arguments[1];
                                          };
                                        })(),
                                        lineno: 150
                                      }));
                                      __iced_deferrals._fulfill();
                                    })(function() {
                                      if (err) {
                                        done(client);
                                        return next(err);
                                      }
                                      return _next(terms_input.push.apply(terms_input, result.rows));
                                    });
                                  }
                                };
                                _while(__iced_k);
                              })(function() {
                                res.locals.notify.terms_input = terms_input;
                                terms_wait_effect = [];
                                (function(__iced_k) {
                                  var _i, _len, _ref, _results, _while;
                                  _ref = req.session.notify.terms_wait_effect;
                                  _len = _ref.length;
                                  _i = 0;
                                  _while = function(__iced_k) {
                                    var _break, _continue, _next;
                                    _break = __iced_k;
                                    _continue = function() {
                                      return iced.trampoline(function() {
                                        ++_i;
                                        return _while(__iced_k);
                                      });
                                    };
                                    _next = _continue;
                                    if (!(_i < _len)) {
                                      return _break();
                                    } else {
                                      term = _ref[_i];
                                      (function(__iced_k) {
                                        __iced_deferrals = new iced.Deferrals(__iced_k, {
                                          parent: ___iced_passed_deferral,
                                          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
                                        });
                                        client.query("select t.*, m.name as mer_name, m.merno as mer_no from pay_terms_ex as t left join pay_merch as m on m.uuid = t.merid where uuid = $1", [term.uuid], __iced_deferrals.defer({
                                          assign_fn: (function() {
                                            return function() {
                                              err = arguments[0];
                                              return result = arguments[1];
                                            };
                                          })(),
                                          lineno: 164
                                        }));
                                        __iced_deferrals._fulfill();
                                      })(function() {
                                        if (err) {
                                          done(client);
                                          return next(err);
                                        }
                                        return _next(terms_wait_effect.push.apply(terms_wait_effect, result.rows));
                                      });
                                    }
                                  };
                                  _while(__iced_k);
                                })(function() {
                                  res.locals.notify.terms_wait_effect = terms_wait_effect;
                                  done();
                                  return res.render('notify/index');
                                });
                              });
                            });
                          });
                        });
                      });
                    });
                  });
                });
              });
            });
          });
        });
      };
    })(this));
  });

  router.post('/notification/delete', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['serial'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    if (!req.session.userinfo.remove_notify) {
      return next(new Error('权限拒绝'));
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 181
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
          });
          client.query("update web_notify set status = 2 where serial = $1", [req.body.serial], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 187
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.json({
            succ: true,
            redirect: '/notify'
          });
        });
      };
    })(this));
  });

  router.post('/notification/clear', function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    if (!req.session.userinfo.remove_notify) {
      return next(new Error('权限拒绝'));
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 199
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/notify.coffee"
          });
          client.query("update web_notify set status = 2", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 203
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.json({
            succ: true,
            redirect: '/notify'
          });
        });
      };
    })(this));
  });

  router.post('/refresh', function(req, res, next) {
    res.type('json');
    req.app.set('notify_modified_time', moment().format('x'));
    return res.json({
      succ: true
    });
  });

}).call(this);
